<?php

namespace humhub\modules\api\models;
use Yii;

/**
 * This is the model class for table "api_oauth".
 */
class ApiAuth extends \yii\db\ActiveRecord 
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'api_oauth';
    }

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['user_id', 'client_id', 'access_token', 'expires_in', 'expires_times', 'active'], 'required'],
        ];
    }

    /**
     * Implements authentication of the user api_key
     * @param String $token
     * @return mixed
     */
    public static function findIdentityByAccessToken($token){
        $times = time();
        //return static::findOne(['access_token'=>$token, 'active'=>1, ['>','expires_times',$times]]);
        return static::find()
            ->where(['access_token' => $token])
            ->andWhere(['>','expires_times', $times])
            ->andWhere(['active' => 1])
            ->one();
    }
}
